<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>
<script>
import BScroll from 'better-scroll'
export default {
  name:'Scroll',
  props:{
    probeType:{
      type:Number,
      default: 0
    },
    pullUpLoad:{
      type:Boolean,
      default:false
    }
 
  },
  data() {
    return {
      scroll:null,
      message:'哈哈哈'
    }
  },
  mounted() {
    // 1.创建BScroll对象
    this.scroll = new BScroll(this.$refs.wrapper,{
      // observeDOM:true,//用这个可以解决下拉加载高度卡顿的问题，也可以用事件总线
      // observeImage:true,
      click:true,
      probeType: this.probeType, //实时监听和不需要实时监听
      pullUpLoad: this.pullUpLoad
    })
    // 2.监听滚动位置
    if(this.probeType === 2 || this.probeType === 3){
      this.scroll.on('scroll',(position) => {
        this.$emit('scroll',position)
      })
    }
    // 3.监听scroll滚动到底部
    if(this.pullUpLoad){
      this.scroll.on('pullingUp',() => {
        this.$emit('pullingUp')
      })
    }

  },
  methods: {
    // 封装scrollTo方法，Home.vue调用
    scrollTo(x,y,time=300){//time给了默认值
      this.scroll && this.scroll.scrollTo && this.scroll.scrollTo(x,y,time)
    },
    refresh(){
      this.scroll && this.scroll.refresh && this.scroll.refresh();
    },
    finishPullUp(){
      this.scroll && this.scroll.finishPullUp()
    },
    getScrollY(){
      return this.scroll ? this.scroll.y : 0
    }
  },
}
</script>
<style scoped>

</style>